package com.prestamomo.vil.ppa.pmvilpage.pmfg;

import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilClient;
import com.prestamomo.vil.ppa.pmvilapi.PmvilGoods;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilnet.PmvilRetrofitHelper;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.PmvilAdviseVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilCofigVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilOwnerVy;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilFgRoot;

public class PmvilCofigFg extends PmvilFgRoot {

    private LinearLayout pmvilCofigLlgoSz;
    private LinearLayout pmvilCofigLlXy;
    private LinearLayout pmvilCofigLlQx;
    private RelativeLayout pmvilCofigRlFecbak;
    private LinearLayout pmvilCofigLlgoOrder;
    private TextView pmvilCofigTvNum;
    private TextView pmvilCofigTvPhone;

    @Override
    public int setPmvilLayoutId() {
        return R.layout.fg_cofig_pmvil_ly;
    }

    @Override
    public void findPmvilViewById(View view) {
        pmvilCofigTvPhone = view.findViewById(R.id.fg_cofig_pmvil_tv_phone);
        pmvilCofigTvNum = view.findViewById(R.id.fg_cofig_pmvil_tv_num);
        pmvilCofigLlgoOrder = view.findViewById(R.id.fg_cofig_pmvil_ll_go_order);

    }

    @Override
    public void setViewPmvilData(View view) {
        pmvilCofigRlFecbak = view.findViewById(R.id.fg_cofig_pmvil_rl_fecbak);
        pmvilCofigLlQx = view.findViewById(R.id.fg_cofig_pmvil_ll_qx);
        pmvilCofigLlXy = view.findViewById(R.id.fg_cofig_pmvil_ll_xy);
        pmvilCofigLlgoSz = view.findViewById(R.id.fg_cofig_pmvil_ll_sz);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPmvilUserData();
    }

    @Override
    public void setClickPmvilEvent(View view) {
        pmvilCofigLlQx.setOnClickListener(v -> pmvilAtyRoot.showPmvilPermisDlg(false,R.layout.permis_pmvil_dlg));
        pmvilCofigLlgoOrder.setOnClickListener(v -> ((PmvilOwnerVy)getActivity()).setPmvilChioce(1));
        pmvilCofigRlFecbak.setOnClickListener(v -> startActivity(new Intent(pmvilAtyRoot, PmvilAdviseVy.class)));
        pmvilCofigLlXy.setOnClickListener(v -> pmvilAtyRoot.goToPmvilXyAty("隱私權政策", PmvilRetrofitHelper.pmvilWebXYUrl));
        pmvilCofigLlgoSz.setOnClickListener(v -> startActivity(new Intent(pmvilAtyRoot, PmvilCofigVy.class)));
    }

    private void getPmvilUserData(){
        pmvilAtyRoot.showPmvilLoading();
        PmvilHttpRequest.getUserPmvilData(this, new PmvilCallBack<PmvilClient>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilClient pmvilContent) {
                pmvilCofigTvPhone.setText(pmvilContent.getMobile());
                getGoodsPmvilList();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilClient pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilClient pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void getGoodsPmvilList(){
        pmvilAtyRoot.showPmvilLoading();
        PmvilHttpRequest.getGoodsPmvilList(this, new PmvilCallBack<PmvilGoods>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilGoods pmvilContent) {
                pmvilAtyRoot.hindPmvilLoading();
                pmvilCofigTvNum.setText(Html.fromHtml("許可您還有 <font color ='#FF7B31'><b>"+pmvilContent.getRepayCount()+"</b></font> 個訂單需要支付"));

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilGoods pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilGoods pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }
}
